package classes.controlers {
	import classes.global.GlobalProperties;
	import classes.managers.Game_Manager;
	import classes.sounds.SoundCollection;
	import flash.display.MovieClip;
	import flash.events.Event;
	/**
	 * ...
	 * @author Kihoonee
	 */
	public class Coin_Controler extends A_Controler {
		private var _coin:int = 0;
		
		private var totalCoin:int = 0;
		
		private var coinEndCheck:int = 0;
		
		public var coin1:Boolean = false;
		public var coin2:Boolean = false;
		public var coin3:Boolean = false;
		private var coinY:Number;
		private var coinX:Number;
		
		private var oldX:Number = 560;
		private var oldY:Number = -222;
		
		public function Coin_Controler( _gm:Game_Manager, _mc:MovieClip ) {
			super( _gm, _mc );
		}
		
		public function init():void {
			//visible = false;
			
			coinEndCheck = 0;
			
			mc.coin10_mc1.gotoAndStop(1); mc.coin10_mc2.gotoAndStop(1); mc.coin10_mc3.gotoAndStop(1);
			mc.coin20_mc1.gotoAndStop(1); mc.coin20_mc2.gotoAndStop(1); mc.coin20_mc3.gotoAndStop(1);
			mc.coin30_mc1.gotoAndStop(1); mc.coin30_mc2.gotoAndStop(1); mc.coin30_mc3.gotoAndStop(1);
			mc.coin40_mc1.gotoAndStop(1); mc.coin40_mc2.gotoAndStop(1); mc.coin40_mc3.gotoAndStop(1);
			mc.coin50_mc1.gotoAndStop(1); mc.coin50_mc2.gotoAndStop(1); mc.coin50_mc3.gotoAndStop(1);
			mc.coin60_mc1.gotoAndStop(1); mc.coin60_mc2.gotoAndStop(1); mc.coin60_mc3.gotoAndStop(1);
			mc.coin100_mc1.gotoAndStop(1); mc.coin100_mc2.gotoAndStop(1); mc.coin100_mc3.gotoAndStop(1);
			mc.coin1000_mc1.gotoAndStop(1); mc.coin1000_mc2.gotoAndStop(1); mc.coin1000_mc3.gotoAndStop(1);
			mc.coin10000_mc1.gotoAndStop(1); mc.coin10000_mc2.gotoAndStop(1); mc.coin10000_mc3.gotoAndStop(1);
			
			visibleManager( 0 );
		}
		
		public function spawnCoin( _coin:int ):void {
			totalCoin = _coin;
			coin = int(_coin/3);
			visibleManager( _coin );
			
			SoundCollection.coinDropSound();
			SoundCollection.coinsDropSound();
		}
		
		public function obtainCoin():void {
			var coinMC:MovieClip;
			
			trace("coin1 : ", coin1)
			trace("coin2 : ", coin2)
			trace("coin3 : ", coin3)
			trace("coin : ", coin);
			
			if ( coin1 ) {
				switch (totalCoin) {
					case 10: coinMC = mc.coin10_mc1;
					break;
					case 20: coinMC = mc.coin20_mc1;
					break;
					case 30: coinMC = mc.coin30_mc1;
					break;
					case 40: coinMC = mc.coin40_mc1;
					break;
					case 50: coinMC = mc.coin50_mc1;
					break;
					case 60: coinMC = mc.coin60_mc1;
					break;
					case 100: coinMC = mc.coin100_mc1;
					break;
					case 1000: coinMC = mc.coin1000_mc1;
					break;
					case 10000: coinMC = mc.coin10000_mc1;
					break;
					default:
				}
				coin1 = false;
				SoundCollection.getCoinSound();
				gm.obtainCoin += coin;
				GlobalProperties.coin = gm.obtainCoin;
			}
			
			if ( coin2 ) {
				switch (totalCoin) {
					case 10: coinMC = mc.coin10_mc2;
					break;
					case 20: coinMC = mc.coin20_mc2;
					break;
					case 30: coinMC = mc.coin30_mc2;
					break;
					case 40: coinMC = mc.coin40_mc2;
					break;
					case 50: coinMC = mc.coin50_mc2;
					break;
					case 60: coinMC = mc.coin60_mc2;
					break;
					case 100: coinMC = mc.coin100_mc2;
					break;
					case 1000: coinMC = mc.coin1000_mc2;
					break;
					case 10000: coinMC = mc.coin10000_mc2;
					break;
					default:
				}
				coin2 = false;
				SoundCollection.getCoinSound();
				gm.obtainCoin += coin;
				GlobalProperties.coin = gm.obtainCoin;
			}
			
			if ( coin3 ) {
				switch (totalCoin) {
					case 10: coinMC = mc.coin10_mc3;
					break;
					case 20: coinMC = mc.coin20_mc3;
					break;
					case 30: coinMC = mc.coin30_mc3;
					break;
					case 40: coinMC = mc.coin40_mc3;
					break;
					case 50: coinMC = mc.coin50_mc3;
					break;
					case 60: coinMC = mc.coin60_mc3;
					break;
					case 100: coinMC = mc.coin100_mc3;
					break;
					case 1000: coinMC = mc.coin1000_mc3;
					break;
					case 10000: coinMC = mc.coin10000_mc3;
					break;
					default:
				}
				coin3 = false;
				SoundCollection.getCoinSound();
				gm.obtainCoin += coin;
				GlobalProperties.coin = gm.obtainCoin;
			}
			
			if ( coinMC == null ) {
			}else {
				trace("coinMC : ", coinMC.name);
				coinMC.stop();
				coinMC.visible = true;
				coinMC.gotoAndPlay("동전먹음");
				coinMC.addEventListener("obtain_END", function(e:Event):void {
					coinMC.removeEventListener("obtain_END", arguments.callee );
					coinMC.removeEventListener("coin_END", coin_END );
					coinMC.visible = false;
					coinMC.x = oldX;
					coinMC.y = oldY;
				});
			}
		}
		
		private function coin_END(e:Event):void {
			var coinMC:MovieClip = e.target as MovieClip;
			
			if ( coinMC.name.split("_")[1] == "mc1" ) {
				coin1 = false;
			}else if ( coinMC.name.split("_")[1] == "mc2" ) {
				coin2 = false;
			}else if ( coinMC.name.split("_")[1] == "mc3" ) {
				coin3 = false;
			}
			
			coinMC.x = oldX; coinMC.y = oldY;
			
			trace('coin_END : ', coinMC.name)
			coinEndCheck++;
			
			if ( coinMC.hasEventListener("coin_END")) {
				coinMC.removeEventListener("coin_END", coin_END );
			}
			
			if ( coinEndCheck === 3 ) {
				trace("이거 뭐지 대체 ;;")
				visibleManager( 0 );
				coinEndCheck = 0;
			}
		}
		
		private function visibleManager( _coin:int):void {
			
			mc.coin10_mc1.visible = false; mc.coin10_mc2.visible = false; mc.coin10_mc3.visible = false;
			mc.coin20_mc1.visible = false; mc.coin20_mc2.visible = false; mc.coin20_mc3.visible = false;
			mc.coin30_mc1.visible = false; mc.coin30_mc2.visible = false; mc.coin30_mc3.visible = false;
			mc.coin40_mc1.visible = false; mc.coin40_mc2.visible = false; mc.coin40_mc3.visible = false;
			mc.coin50_mc1.visible = false; mc.coin50_mc2.visible = false; mc.coin50_mc3.visible = false;
			mc.coin60_mc1.visible = false; mc.coin60_mc2.visible = false; mc.coin60_mc3.visible = false;
			mc.coin100_mc1.visible = false; mc.coin100_mc2.visible = false; mc.coin100_mc3.visible = false;
			mc.coin1000_mc1.visible = false; mc.coin1000_mc2.visible = false; mc.coin1000_mc3.visible = false;
			mc.coin10000_mc1.visible = false; mc.coin10000_mc2.visible = false; mc.coin10000_mc3.visible = false;
			
			mc.coin10_mc1.gotoAndStop(1); mc.coin10_mc2.gotoAndStop(1); mc.coin10_mc3.gotoAndStop(1);
			mc.coin20_mc1.gotoAndStop(1); mc.coin20_mc2.gotoAndStop(1); mc.coin20_mc3.gotoAndStop(1);
			mc.coin30_mc1.gotoAndStop(1); mc.coin30_mc2.gotoAndStop(1); mc.coin30_mc3.gotoAndStop(1);
			mc.coin40_mc1.gotoAndStop(1); mc.coin40_mc2.gotoAndStop(1); mc.coin40_mc3.gotoAndStop(1);
			mc.coin50_mc1.gotoAndStop(1); mc.coin50_mc2.gotoAndStop(1); mc.coin50_mc3.gotoAndStop(1);
			mc.coin60_mc1.gotoAndStop(1); mc.coin60_mc2.gotoAndStop(1); mc.coin60_mc3.gotoAndStop(1);
			mc.coin100_mc1.gotoAndStop(1); mc.coin100_mc2.gotoAndStop(1); mc.coin100_mc3.gotoAndStop(1);
			mc.coin1000_mc1.gotoAndStop(1); mc.coin1000_mc2.gotoAndStop(1); mc.coin1000_mc3.gotoAndStop(1);
			mc.coin10000_mc1.gotoAndStop(1); mc.coin10000_mc2.gotoAndStop(1); mc.coin10000_mc3.gotoAndStop(1);
			
			coin1 = false;
			coin2 = false;
			coin3 = false;
			
			coinEndCheck = 0;
			
			ranX();
			ranY();
			
			switch (_coin) {
				case 10: mc.coin10_mc1.visible = true; mc.coin10_mc1.num_txt.text = coin; mc.coin10_mc1.gotoAndPlay("동전떨어지기"); mc.coin10_mc1.x = coinX; mc.coin10_mc1.y = coinY;
						 mc.coin10_mc1.addEventListener("coin_END", coin_END ); mc.coin10_mc1.hit_mc.visible = true; //coin1 = true; 
						 ranX(); ranY();
						 mc.coin10_mc2.addEventListener("coin_END", coin_END ); 
						 mc.coin10_mc2.visible = true; mc.coin10_mc2.num_txt.text = coin; mc.coin10_mc2.gotoAndPlay("동전떨어지기"); mc.coin10_mc2.x = coinX; mc.coin10_mc2.y = coinY;
						 mc.coin10_mc2.hit_mc.visible = true;  //coin5 = true;
						 ranX(); ranY();
						 mc.coin10_mc3.visible = true; mc.coin10_mc3.num_txt.text = coin; mc.coin10_mc3.gotoAndPlay("동전떨어지기"); mc.coin10_mc3.x = coinX; mc.coin10_mc3.y = coinY;
						 mc.coin10_mc3.addEventListener("coin_END", coin_END ); mc.coin10_mc3.hit_mc.visible = true;  //coin10 = true;
				break;
				case 20: mc.coin20_mc1.visible = true; mc.coin20_mc1.num_txt.text = coin; mc.coin20_mc1.gotoAndPlay("동전떨어지기"); mc.coin20_mc1.x = coinX; mc.coin20_mc1.y = coinY;
						 mc.coin20_mc1.addEventListener("coin_END", coin_END ); mc.coin20_mc1.hit_mc.visible = true;  //coin1 = true;
						 ranX(); ranY();
						 mc.coin20_mc2.visible = true; mc.coin20_mc2.num_txt.text = coin; mc.coin20_mc2.gotoAndPlay("동전떨어지기"); mc.coin20_mc2.x = coinX; mc.coin20_mc2.y = coinY;
						 mc.coin20_mc2.addEventListener("coin_END", coin_END ); mc.coin20_mc2.hit_mc.visible = true;  //coin5 = true;
						 ranX(); ranY();
						 mc.coin20_mc3.visible = true; mc.coin20_mc3.num_txt.text = coin; mc.coin20_mc3.gotoAndPlay("동전떨어지기"); mc.coin20_mc3.x = coinX; mc.coin20_mc3.y = coinY;
						 mc.coin20_mc3.addEventListener("coin_END", coin_END ); mc.coin20_mc3.hit_mc.visible = true;  //coin10 = true;
				break;
				case 30: mc.coin30_mc1.visible = true; mc.coin30_mc1.num_txt.text = coin; mc.coin30_mc1.gotoAndPlay("동전떨어지기"); mc.coin30_mc1.x = coinX; mc.coin30_mc1.y = coinY;
						 mc.coin30_mc1.addEventListener("coin_END", coin_END ); mc.coin30_mc1.hit_mc.visible = true; //coin1 = true;
						 ranX(); ranY();
						 mc.coin30_mc2.visible = true; mc.coin30_mc2.num_txt.text = coin; mc.coin30_mc2.gotoAndPlay("동전떨어지기"); mc.coin30_mc2.x = coinX; mc.coin30_mc2.y = coinY;
						 mc.coin30_mc2.addEventListener("coin_END", coin_END ); mc.coin30_mc2.hit_mc.visible = true; //coin5 = true;
						 ranX(); ranY();
						 mc.coin30_mc3.visible = true; mc.coin30_mc3.num_txt.text = coin; mc.coin30_mc3.gotoAndPlay("동전떨어지기"); mc.coin30_mc3.x = coinX; mc.coin30_mc3.y = coinY;
						 mc.coin30_mc3.addEventListener("coin_END", coin_END ); mc.coin30_mc3.hit_mc.visible = true; //coin10 = true;
				break;
				case 40:  mc.coin40_mc1.visible = true; mc.coin40_mc1.num_txt.text = coin; mc.coin40_mc1.gotoAndPlay("동전떨어지기"); mc.coin40_mc1.x = coinX; mc.coin40_mc1.y = coinY;
						 mc.coin40_mc1.addEventListener("coin_END", coin_END ); mc.coin40_mc1.hit_mc.visible = true; //coin1 = true;
						 ranX(); ranY();
						 mc.coin40_mc2.visible = true; mc.coin40_mc2.num_txt.text = coin; mc.coin40_mc2.gotoAndPlay("동전떨어지기"); mc.coin40_mc2.x = coinX; mc.coin40_mc2.y = coinY;
						 mc.coin40_mc2.addEventListener("coin_END", coin_END ); mc.coin40_mc2.hit_mc.visible = true; //coin5 = true;
						 ranX(); ranY();
						 mc.coin40_mc3.visible = true; mc.coin40_mc3.num_txt.text = coin; mc.coin40_mc3.gotoAndPlay("동전떨어지기"); mc.coin40_mc3.x = coinX; mc.coin40_mc3.y = coinY;
						 mc.coin40_mc3.addEventListener("coin_END", coin_END ); mc.coin40_mc3.hit_mc.visible = true; //coin10 = true;
				break;
				case 50:  mc.coin50_mc1.visible = true; mc.coin50_mc1.num_txt.text = coin; mc.coin50_mc1.gotoAndPlay("동전떨어지기"); mc.coin50_mc1.x = coinX; mc.coin50_mc1.y = coinY;
						 mc.coin50_mc1.addEventListener("coin_END", coin_END ); mc.coin50_mc1.hit_mc.visible = true; //coin1 = true;
						 ranX(); ranY();
						 mc.coin50_mc2.visible = true; mc.coin50_mc2.num_txt.text = coin; mc.coin50_mc2.gotoAndPlay("동전떨어지기"); mc.coin50_mc2.x = coinX; mc.coin50_mc2.y = coinY;
						 mc.coin50_mc2.addEventListener("coin_END", coin_END ); mc.coin50_mc2.hit_mc.visible = true; //coin5 = true;
						 ranX(); ranY();
						 mc.coin50_mc3.visible = true; mc.coin50_mc3.num_txt.text = coin; mc.coin50_mc3.gotoAndPlay("동전떨어지기"); mc.coin50_mc3.x = coinX; mc.coin50_mc3.y = coinY;
						 mc.coin50_mc3.addEventListener("coin_END", coin_END ); mc.coin50_mc3.hit_mc.visible = true; //coin10 = true;
				break;
				case 60:  mc.coin60_mc1.visible = true; mc.coin60_mc1.num_txt.text = coin; mc.coin60_mc1.gotoAndPlay("동전떨어지기"); mc.coin60_mc1.x = coinX; mc.coin60_mc1.y = coinY;
						 mc.coin60_mc1.addEventListener("coin_END", coin_END ); mc.coin60_mc1.hit_mc.visible = true; //coin1 = true;
						 ranX(); ranY();
						 mc.coin60_mc2.visible = true; mc.coin60_mc2.num_txt.text = coin; mc.coin60_mc2.gotoAndPlay("동전떨어지기"); mc.coin60_mc2.x = coinX; mc.coin60_mc2.y = coinY;
						 mc.coin60_mc2.addEventListener("coin_END", coin_END ); mc.coin60_mc2.hit_mc.visible = true; //coin5 = true;
						 ranX(); ranY();
						 mc.coin60_mc3.visible = true; mc.coin60_mc3.num_txt.text = coin; mc.coin60_mc3.gotoAndPlay("동전떨어지기"); mc.coin60_mc3.x = coinX; mc.coin60_mc3.y = coinY;
						 mc.coin60_mc3.addEventListener("coin_END", coin_END ); mc.coin60_mc3.hit_mc.visible = true; //coin10 = true;
				break;
				case 100: mc.coin100_mc1.visible = true; mc.coin100_mc1.num_txt.text = coin; mc.coin100_mc1.gotoAndPlay("동전떨어지기"); mc.coin100_mc1.x = coinX; mc.coin100_mc1.y = coinY;
						 mc.coin100_mc1.addEventListener("coin_END", coin_END ); mc.coin100_mc1.hit_mc.visible = true; //coin1 = true;
						 ranX(); ranY();
						 mc.coin100_mc2.visible = true; mc.coin100_mc2.num_txt.text = coin; mc.coin100_mc2.gotoAndPlay("동전떨어지기"); mc.coin100_mc2.x = coinX; mc.coin100_mc2.y = coinY;
						 mc.coin100_mc2.addEventListener("coin_END", coin_END ); mc.coin100_mc2.hit_mc.visible = true; //coin5 = true;
						 ranX(); ranY();
						 mc.coin100_mc3.visible = true; mc.coin100_mc3.num_txt.text = coin; mc.coin100_mc3.gotoAndPlay("동전떨어지기"); mc.coin100_mc3.x = coinX; mc.coin100_mc3.y = coinY;
						 mc.coin100_mc3.addEventListener("coin_END", coin_END ); mc.coin100_mc3.hit_mc.visible = true; //coin10 = true;
				break;
				case 1000: mc.coin1000_mc1.visible = true; mc.coin1000_mc1.num_txt.text = coin; mc.coin1000_mc1.gotoAndPlay("동전떨어지기"); mc.coin1000_mc1.x = coinX; mc.coin1000_mc1.y = coinY;
						 mc.coin1000_mc1.addEventListener("coin_END", coin_END ); mc.coin1000_mc1.hit_mc.visible = true; //coin1 = true;
						 ranX(); ranY();
						 mc.coin1000_mc2.visible = true; mc.coin1000_mc2.num_txt.text = coin; mc.coin1000_mc2.gotoAndPlay("동전떨어지기"); mc.coin1000_mc2.x = coinX; mc.coin1000_mc2.y = coinY;
						 mc.coin1000_mc2.addEventListener("coin_END", coin_END ); mc.coin1000_mc2.hit_mc.visible = true; //coin5 = true;
						 ranX(); ranY();
						 mc.coin1000_mc3.visible = true; mc.coin1000_mc3.num_txt.text = coin; mc.coin1000_mc3.gotoAndPlay("동전떨어지기"); mc.coin1000_mc3.x = coinX; mc.coin1000_mc3.y = coinY;
						 mc.coin1000_mc3.addEventListener("coin_END", coin_END ); mc.coin1000_mc3.hit_mc.visible = true; //coin10 = true;
						 SoundCollection.jackPotCoinsDropSound();
				break;
				case 10000: mc.coin10000_mc1.visible = true; mc.coin10000_mc1.num_txt.text = coin; mc.coin10000_mc1.gotoAndPlay("동전떨어지기"); mc.coin10000_mc1.x = coinX; mc.coin10000_mc1.y = coinY;
						 mc.coin10000_mc1.addEventListener("coin_END", coin_END ); mc.coin10000_mc1.hit_mc.visible = true; //coin1 = true;
						 ranX(); ranY();
						 mc.coin10000_mc2.visible = true; mc.coin10000_mc2.num_txt.text = coin; mc.coin10000_mc2.gotoAndPlay("동전떨어지기"); mc.coin10000_mc2.x = coinX; mc.coin10000_mc2.y = coinY;
						 mc.coin10000_mc2.addEventListener("coin_END", coin_END ); mc.coin10000_mc2.hit_mc.visible = true; //coin5 = true;
						 ranX(); ranY();
						 mc.coin10000_mc3.visible = true; mc.coin10000_mc3.num_txt.text = coin; mc.coin10000_mc3.gotoAndPlay("동전떨어지기"); mc.coin10000_mc3.x = coinX; mc.coin10000_mc3.y = coinY;
						 mc.coin10000_mc3.addEventListener("coin_END", coin_END ); mc.coin10000_mc3.hit_mc.visible = true; //coin10 = true;
						 SoundCollection.jackPotCoinsDropSound();
				break;
				default:
			}
		}
		
		private function ranX():void {
			coinX = Math.random() * (mc.parent["mask_mc"].width - 60) + 20;
			
			if ( coinX > mc.parent["coinNo_mc"].x && coinX < mc.parent["coinNo_mc"].x + mc.parent["coinNo_mc"].width ) {
				ranX();
			}
		}
		
		private function ranY():void {
			coinY = Math.random() * (mc.parent["mask_mc"].height-80 - 50) + 40 + 50;
			
			if ( coinY > mc.parent["coinNo_mc"].y && coinY < mc.parent["coinNo_mc"].y + mc.parent["coinNo_mc"].height ) {
				ranY();
			}
		}
		
		
		
		public function get coin():int {
			return _coin;
		}
		
		public function set coin(value:int):void {
			_coin = value;
		}
	}
}